package logic.framework_logic;

import java.util.LinkedList;
import java.util.List;

import logic.utility_logic.CourseQueryLogicsvFactory;
import logicsv.courseQuery_logicsv.CourseQueryLogicsv;
import po.ModelPO;
import po.SubModelPO;
import resultmessage.FrameworkRM;
import vo.CourseVO;
import vo.ModelVO;
import vo.SubModelVO;
import datasv.courseQuery_datasv.CourseQueryDatasv;
import datasv.datafactorysv.CourseQueryDatasvFactory;
import datasv.datafactorysv.FrameworkDatasvFactory;
import datasv.framework_datasv.FrameworkDatasv;
import datasv.framework_datasv.FrameworkDatasvController;

public class Framework {
	FrameworkDatasv frameworkData = new FrameworkDatasvController();
	
	public Framework() {
		super();
		FrameworkDatasvFactory fdf = new FrameworkDatasvFactory();
		frameworkData = fdf.create();
		// TODO Auto-generated constructor stub
	}

	public FrameworkRM addModel(String collegeID, ModelPO m) {
		// TODO Auto-generated method stub
		return frameworkData.addModel(collegeID, m);
	}

	public ModelVO getModel(String collegeID, String modelID) {
		// TODO Auto-generated method stub
		ModelPO mp = frameworkData.getModel(collegeID, modelID);
		List<SubModelPO> subModelList = frameworkData.getAllSubModelOfModel(collegeID, modelID);
		LinkedList<SubModelVO> smvList = new LinkedList<SubModelVO>();
		for(SubModelPO smp : subModelList){
			smvList.add(this.getSubModel(collegeID, modelID, smp.getSubModelID()));
		}
		return new ModelVO(mp,smvList);
	}
	public FrameworkRM deleteModel(String collegeID, String modelID) {
		// TODO Auto-generated method stub
		return frameworkData.deleteModel(collegeID, modelID);
	}

	public FrameworkRM updateModel(String collegeID, ModelPO m) {
		// TODO Auto-generated method stub
		return frameworkData.updateModel(collegeID, m);
	}

	public FrameworkRM addSubModel(String collegeID, String modelID,
			SubModelPO sm) {
		// TODO Auto-generated method stub
		return frameworkData.addSubModel(collegeID, modelID, sm);
	}

	public SubModelVO getSubModel(String collegeID, String modelID,
			String subModelID) {
		// TODO Auto-generated method stub
		CourseQueryDatasv courseQD = new CourseQueryDatasvFactory().create();
		List<String> subModelCourseList  = courseQD.getAllCouOfSubModel(collegeID, modelID,subModelID);
		SubModelPO smp = frameworkData.getSubModel(collegeID, modelID, subModelID);
		return new SubModelVO(smp,getCourseOfSubModel(subModelCourseList));
	}

	private List<CourseVO> getCourseOfSubModel(List<String> courseIDList){
		CourseQueryLogicsv courseQL = new CourseQueryLogicsvFactory().create();
		LinkedList<CourseVO> list = new LinkedList<CourseVO>();
		for(String id : courseIDList){
			list.add(courseQL.getCourse(id));
		}
		return list;
	}

	public FrameworkRM deleteSubModel(String collegeID, String modelID,
			String subModelID) {
		// TODO Auto-generated method stub
		return frameworkData.deleteSubModel(collegeID, modelID, subModelID);
	}

	public FrameworkRM updateSubModel(String collegeID, String modelID,
			SubModelPO sm) {
		// TODO Auto-generated method stub
		return frameworkData.updateSubModel(collegeID, modelID, sm);
	}
}
